﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Physis.Client.Controllers {

    public class SponsorController : ApplicationController {

        #region Index/Workspace

        public ActionResult Index () {

            return View ("~/Views/Sponsor/SponsorWorkspace.cshtml", applicationModel);

        }

        public ActionResult Workspace () { return Index (); }

        #endregion


        #region Grid Queries

        public JsonResult SponsorTypesForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Sponsor.SponsorType> sponsorTypes = applicationModel.Application.Core.Sponsor.SponsorTypes ();

            response.RowsByPage (sponsorTypes);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        #endregion 

        
        #region Sponsor Types

        [HttpGet]
        public JsonResult SponsorTypes (Boolean useCaching = true) {

            JsonResponses.ObjectCollectionResponse response = new JsonResponses.ObjectCollectionResponse ();

            response.Objects = applicationModel.Application.Core.Sponsor.SponsorTypes (useCaching);

            response.SetException (applicationModel.Application.LastException);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        [HttpGet]
        public ActionResult SponsorType (Int64 id) {

            Models.Sponsor.SponsorTypeModel model = new Models.Sponsor.SponsorTypeModel (id);

            return View ("~/Views/Sponsor/SponsorType.cshtml", model);

        }

        [HttpPost]
        public JsonResult SponsorTypeSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Sponsor.SponsorTypeModel model = new Models.Sponsor.SponsorTypeModel (Request.Form);

            response.Result = model.SponsorType.Save ();

            response.Id = model.SponsorType.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult SponsorTypeDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Sponsor.SponsorTypeModel model = new Models.Sponsor.SponsorTypeModel (id);

            response.Result = model.SponsorType.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 


        #region Sponsors

        [HttpGet]
        public JsonResult SponsorSearch (String criteria) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Data.FilterDescriptor> filters = new List<Data.FilterDescriptor> ();

            
            filters.Add (new Data.FilterDescriptor ("Name", Data.Enumerations.DataFilterOperator.Contains, criteria.Replace ("'", "''")));

            List<Core.Sponsor.Sponsor> sponsors = applicationModel.Application.Core.Sponsor.SponsorsByPage (filters, null, 1, 999999);

            response.RowsByPage (sponsors);


            return Json (response, JsonRequestBehavior.AllowGet);

        }

        [HttpGet]
        public JsonResult SponsorAutoComplete (String term) {

            List<Data.FilterDescriptor> filters = new List<Data.FilterDescriptor> ();


            filters.Add (new Data.FilterDescriptor ("Name", Data.Enumerations.DataFilterOperator.StartsWith, term.Replace ("'", "''")));

            List<Core.Sponsor.Sponsor> sponsors = applicationModel.Application.Core.Sponsor.SponsorsByPage (filters, null, 1, 999999);

            var response = (from currentSponsor in sponsors

                            select new { label = currentSponsor.Name, value = currentSponsor.Id }).ToArray ();

            return Json (response, JsonRequestBehavior.AllowGet);

        }


        [HttpGet]
        public ActionResult Sponsor (Int64 id) {

            Models.Sponsor.SponsorModel model = new Models.Sponsor.SponsorModel (id);

            String viewName = "~/Views/Sponsor/Sponsor.cshtml";

            if (model.Sponsor.Id == 0) { viewName = "~/Views/Sponsor/SponsorAddWizard.cshtml"; }

            return View (viewName, model);

        }

        [HttpPost]
        public JsonResult SponsorSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Sponsor.SponsorModel model = new Models.Sponsor.SponsorModel (Request.Form);

            response.Result = model.Sponsor.Save ();

            response.Id = model.Sponsor.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 


        #region Sponsor Enrollment 

        [HttpGet]
        public ActionResult SponsorEnroll (Int64 id) {

            Models.Sponsor.SponsorEnrollModel model = new Models.Sponsor.SponsorEnrollModel (id);            

            String viewName = "~/Views/Sponsor/SponsorEnroll.cshtml";

            return View (viewName, model);

        }

        [HttpPost]
        public JsonResult SponsorEnroll () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Sponsor.SponsorEnrollModel model = new Models.Sponsor.SponsorEnrollModel (Request.Form);

            response.Result = model.Enroll (Request.Form);

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpGet]
        public JsonResult SponsorEnrollAvailableParentAutoComplete (String effectiveDate, Int64 childSponsorTypeId, String term) {

            List<Data.FilterDescriptor> filters = new List<Data.FilterDescriptor> ();


            filters.Add (new Data.FilterDescriptor ("Name", Data.Enumerations.DataFilterOperator.StartsWith, term.Replace ("'", "''")));

            List<Core.Sponsor.Sponsor> sponsors = applicationModel.Application.Core.Sponsor.SponsorsByPage (filters, null, 1, 999999);

            var response = (from currentSponsor in sponsors

                            where currentSponsor.SponsorType.AllowedChild (childSponsorTypeId)

                            select new { label = currentSponsor.Name, value = currentSponsor.Id }).ToArray ();

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        [HttpGet]
        public JsonResult SponsorEnrollProgramsByParentSponsor (String effectiveDate, Int64 parentSponsorId) {

            JsonResponses.ObjectCollectionResponse response = new JsonResponses.ObjectCollectionResponse ();

            DateTime convertedEffectiveDate = DateTime.Now;

            DateTime.TryParse (effectiveDate, out convertedEffectiveDate); 


            response.Objects = (from currentSponsorEnrollment in applicationModel.Application.Core.Sponsor.SponsorEnrollments (parentSponsorId, false)

                                where ((currentSponsorEnrollment.Voided == false) // NOT VOIDED 

                                    && (currentSponsorEnrollment.ContainsDate (convertedEffectiveDate))) // CONTAINS THE EFFECTIVE DATE 

                                select new { Name = currentSponsorEnrollment.ProgramName, Id = currentSponsorEnrollment.ProgramId }).ToArray ();



            return Json (response, JsonRequestBehavior.AllowGet);

        }

        #endregion 


    }

}